package com.cos.hospital.ui.main.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cos.hospital.R;
import com.cos.hospital.code.Logic;
import com.cos.hospital.model.bean.AcupointBean;
import com.cos.hospital.model.bean.PatientListBean;
import com.cos.hospital.tool.UDate;
import com.cos.hospital.widget.MyGridLayoutManager;

import java.util.Date;
import java.util.List;

/**
 * 治疗管理适配器
 * Created by yby on 2018/9/10
 */
public class CureAdapter extends BaseQuickAdapter<PatientListBean.ItemsBean, BaseViewHolder> {


    public CureAdapter(@Nullable List<PatientListBean.ItemsBean> data) {
        super(R.layout.item_cure, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, PatientListBean.ItemsBean item) {
        Context context = helper.itemView.getContext();
        helper.setText(R.id.tv_name, item.getFldPatientName());
        helper.setText(R.id.tv_age, item.getFldAge() + "岁");
        helper.setText(R.id.tv_total, item.getFldNum() + "支");
        helper.setText(R.id.tv_other, item.getFldTreatmentDesc());
        helper.setText(R.id.tv_gender, Logic.getSex(item.getFldPatientSex()));
        helper.setVisible(R.id.iv_wechat, item.getFldRegisterType() == 0);
        int fldTreatmentState = item.getFldTreatmentState();
        String state;
        switch (fldTreatmentState) {
            case 1:
                state = "治疗中";
                break;
            case 2:
                state = "治疗结束";
                break;
            default:
                state = "无状态";
                break;
        }
        helper.setText(R.id.tv_status, state);
        if (item.isSearchResult()) {
            helper.itemView.setBackgroundResource(R.color.search_light);
        } else {
            helper.itemView.setBackgroundResource(R.drawable.item_selector);
        }

        RecyclerView rc_acupoint = helper.getView(R.id.rc_acupoint);
        List<AcupointBean> patientTreatmentInfoAcupoint = item.getPatientTreatmentInfoAcupoint();
        AdapterAcupoint adapterAcupoint = new AdapterAcupoint(patientTreatmentInfoAcupoint);
        MyGridLayoutManager myGridLayoutManager = new MyGridLayoutManager(context, 4);
        myGridLayoutManager.setScrollEnabled(false);
        rc_acupoint.setLayoutManager(myGridLayoutManager);
        rc_acupoint.setAdapter(adapterAcupoint);

        String fldTreatmentStart = item.getFldTreatmentStart();
        int fldTreatmentDuration = item.getFldTreatmentDuration();
        Date date = UDate.strToDateSafety(fldTreatmentStart);
        long surplus = date.getTime() / 1000 + fldTreatmentDuration * 60 - System.currentTimeMillis() / 1000;//剩余时间（秒）
        TextView tv_surplus_time = helper.getView(R.id.tv_surplus_time);
        TextView tv_status = helper.getView(R.id.tv_status);
        if (item.isEnd() || fldTreatmentState == 2) {//本地判断结束或者后台返回结束
            if (fldTreatmentState == 2) {//后台返回结束才显示状态
                tv_status.setVisibility(View.VISIBLE);
                tv_surplus_time.setVisibility(View.GONE);
            }
        } else {
            tv_status.setVisibility(View.GONE);
            tv_surplus_time.setVisibility(View.VISIBLE);
            if (surplus > 0) {//剩余时间大于0秒
                long minutes = surplus / 60;
                long seconds = surplus % 60;
                String m = minutes < 10 ? "0" + minutes : minutes + "";
                String s = seconds < 10 ? "0" + seconds : seconds + "";
                tv_surplus_time.setText(m + ":" + s);
            } else {
                tv_surplus_time.setText("00:00");
            }
        }
    }

    public class AdapterAcupoint extends BaseQuickAdapter<AcupointBean, BaseViewHolder> {

        public AdapterAcupoint(@Nullable List<AcupointBean> data) {
            super(R.layout.item_acupoint_show, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, AcupointBean item) {
            helper.setText(R.id.rec_acu_name, item.getFldAcupointName());
            //双侧 = 0,左侧 = 1,右侧 = 2,单侧 = 3
            switch (item.getFldAcupointType()) {
                case 0:
                    helper.getView(R.id.rec_acu_i_l).setVisibility(View.VISIBLE);
                    helper.getView(R.id.rec_acu_i_r).setVisibility(View.VISIBLE);
                    break;
                case 1:
                    helper.getView(R.id.rec_acu_i_l).setVisibility(View.VISIBLE);
                    helper.getView(R.id.rec_acu_i_r).setVisibility(View.GONE);
                    break;
                case 2:
                    helper.getView(R.id.rec_acu_i_l).setVisibility(View.GONE);
                    helper.getView(R.id.rec_acu_i_r).setVisibility(View.VISIBLE);
                    break;
                case 3:
                    helper.getView(R.id.rec_acu_i_l).setVisibility(View.GONE);
                    helper.getView(R.id.rec_acu_i_r).setVisibility(View.GONE);
                    break;
            }
        }
    }
}
